<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script type="module">
    import { obj2ast, transformer, parseComment } from './obj-ast.js';

    const test1 = `
    import { defineRoute } from '@/plugins'

export default defineRoute([
  // 首页
  // #ifdef APP
  {
    disabled: true,
    path: 'pages/tabbar/index',
    style: {
      navigationBarTitleText: "首页"
    }
  },
  // #endif
  {
    path: 'pages/tabbar/task',
    style: {
      navigationBarTitleText: '任务2222'
    }
  },
  {
    path: 'pages/tabbar/my',
    style: {
      navigationBarTitleText: '我的'
    }
  },
  {
    path: 'pages/tabbar/test',
    style: {
      navigationBarTitleText: '我的'
    }
  }, // #ifdef APP
  {
    // ifdef path
    path: 'pages/tabbar/test2',
    // endif path
    style: {
      navigationBarTitleText: '我的'
    }
  }
  // #endif
])
    `
    const test2 = `
import { defineUniPages } from '../../../plugins'

export default defineUniPages({
  include: [false,1],
  exclude: [],
  globalStyle: {
    navigationBarTextStyle: 'white',
    navigationBarTitleText: '司机配送',
    navigationBarBackgroundColor: '#4a9ff8',
    backgroundColor: '#4a9ff8',
  },
  homePage: 'pages/tabbar/my',
  tabBar: {
    color: '#666',
    // 6666666666666
    selectedColor: '#4a9ff8',
    // #ifdef VUE3
    backgroundColor: '#f7f7f7',
    // #endif
    borderStyle: 'white',
    list: [
      // #ifdef APP
      {
        pagePath: 'pages/index/index',
        iconPath: 'static/images/icon-homeed.png',
        selectedIconPath: 'static/images/icon-home.png',
        text: '首页',
      },
      // #endif
      // #ifdef H5
      {
        pagePath: 'pages/task/task',
        iconPath: 'static/images/icon-tasked.png',
        selectedIconPath: 'static/images/icon-task.png',
        text: '任务',
      },
      // #endif
      // #ifdef MP
      {
        pagePath: 'pages/my/my',
        iconPath: 'static/images/icon-myed.png',
        selectedIconPath: 'static/images/icon-my.png',
        text: '我的',
      },
      // endif
    ],
  },
})

`
const test3 = `
import { defineRoute } from '@/plugins'

export default defineRoute(
  {
    root: 'pages/demo',
    pages: [
      {
        path: 'demo1',
        style: {
          navigationStyle: 'default',
          navigationBarTitleText: '首页1',
          enablePullDownRefresh: false,
          navigationBarTextStyle: 'black',
          navigationBarBackgroundColor: '#fff',
        }
      },
      {
        path: 'demo2',
        style: {
          navigationBarTitleText: '任务2'
        }
      },
      {
        path: 'demo3',
        style: {
          navigationBarTitleText: '我的'
        }
      },
      {
        path: 'demo4',
        style: {
          navigationBarTitleText: '我的'
        }
      }
    ]
  }
)

`    
const aa = `{
      a: "\\"1①🍐💩💉💧🐮🍺"
    }`
    const bb = `[
      '// #ifdef APP',
      {
        pagePath: 'pages/index/index',
        iconPath_PLACEHOLDER: '// #endif append_',
        iconPath: 'static/images/icon-homeed.png',
        selectedIconPath: 'static/images/icon-home.png',
        text: '首页',
      },
      '// #endif',
      '// #ifdef H5',
      {
        pagePath: 'pages/task/task',
        iconPath: 'static/images/icon-tasked.png',
        selectedIconPath: 'static/images/icon-task.png',
        text: '任务',
      },
      '// #endif',
      '// #ifdef MP',
      {
        pagePath: 'pages/my/my',
        iconPath: 'static/images/icon-myed.png',
        selectedIconPath: 'static/images/icon-my.png',
        text: '我的',
      },
      '// endif'
    ]`
    const cc = `[
      // #ifdef APP
      {
        pagePath: 'pages/index/index',
        // #endif append_
        iconPath: 'static/images/icon-homeed.png',
        selectedIconPath: 'static/images/icon-home.png',
        text: '首页',
      },
      // #endif
      // #ifdef H5
      {
        pagePath: 'pages/task/task',
        iconPath: 'static/images/icon-tasked.png',
        selectedIconPath: 'static/images/icon-task.png',
        text: '任务',
      },
      // #endif
      // #ifdef MP
      {
        pagePath: 'pages/my/my',
        iconPath: 'static/images/icon-myed.png',
        selectedIconPath: 'static/images/icon-my.png',
        text: '我的',
      },
      // endif
    ]`
    console.time()
    const { ast, json } = obj2ast(test2)
    console.log(ast)
    console.log(json)

    /**
  * 递归格式化 AST
  * @param {Object} node - AST 节点
  * @param {number} indentLevel - 当前缩进层级
  * @returns {string} - 格式化后的字符串
  */
    function formatNode(node, indentLevel = 0) {
      const indent = "  ".repeat(indentLevel); // 根据缩进层级生成空格
      const childIndent = "  ".repeat(indentLevel + 1); // 子节点缩进

      if (node.type === "Object") {
        // 处理对象
        let hasRemove = false;
        const before = node.before
          ? node.before.map(c => `${indent}${c.value}`).join('\n') // 注释缩进
          : '';
        const after = node.after
          ? node.after.map(c => `${indent}${c.value}`).join('\n') // 注释缩进
          : '';
        const properties = node.children
          .map(child => {
            if (child.type === "Property") {
              if (child.key === 'disabled') {
                if (child.value.value === true) hasRemove = true
              }
              // 处理属性，将 key 转换为双引号
              const key = `"${child.key}"`; // 使用双引号包裹 key
              const value = formatNode(child.value, indentLevel + 1);
              const before = child.before
                ? child.before.map(c => `${childIndent}${c.value}`).join('\n') // 注释缩进
                : '';
              const after = child.after
                ? child.after.map(c => `${childIndent}${c.value}`).join('\n') // 注释缩进
                : '';
              return `${before}\n${childIndent}${key}: ${value}${after}`;
            }
            return "";
          })
          .filter(Boolean) // 过滤空值
          .join(",\n");
        if (hasRemove) {
          return ''
        }
        return `${before}\n{\n${properties}\n${indent}}\n${after}`;
      } else if (node.type === "Primitive") {
        // 处理原始值
        return JSON.stringify(node.value);
      } else if (node.type === "Array") {

        // 处理数组
        const items = node.children
          .map(child => {
            // const before = child.before
            //   ? child.before.map(c => `${childIndent}${c.value}`).join('\n') // 注释缩进
            //   : '';
            // const after = child.after
            //   ? child.after.map(c => `${childIndent}${c.value}`).join('\n') // 注释缩进
            //   : '';
            const value = formatNode(child, indentLevel + 1);
            if (!value) return
            // return `${before}\n${childIndent}${value}${after}`;
            return `\n${childIndent}${value}`;
          }).filter(Boolean).join(",\n");
        return `[\n${items}\n${indent}]`;
      } else {
        // 其他类型直接返回空对象
        return "{}";
      }
    }

    const out = formatNode(ast)
    console.log({ out });
    console.timeEnd()
  </script>
</body>

</html>
